package com.rongxun.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.bumptech.glide.Glide;
import com.rongxun.hbwallet.MyApp;
import com.rongxun.hbwallet.R;
import com.rongxun.utils.StatusBarUtil;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 创建者     庄丰泽
 * 创建时间   2018/5/18 12:56
 * 描述
 */

public abstract class BaseActivity extends AppCompatActivity {

    protected static final String TAG = BaseActivity.class.getSimpleName();

    protected boolean existActivityWithAnimation = true;
    private Unbinder bind;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(getContentView());
        MyApp.getInstance().addActivity(this);
        StatusBarUtil.setStatusBarLightMode(this);
        init();

    }


    @Override
    public void setContentView(int layoutResID) {
        if (layoutResID != 0){
            super.setContentView(layoutResID);
        }

        bind = ButterKnife.bind(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        bind.unbind();
        MyApp.getInstance().removeActivity(this);
        try {
            Glide.get(this).clearMemory();
        } catch (Exception pE) {
            pE.printStackTrace();
        }
    }


    protected abstract void init();

    protected abstract int getContentView();


    public void initToolBar(Toolbar toolbar, boolean backPressed) {
        if (toolbar == null) {
            return;
        }
        setSupportActionBar(toolbar);
        ActionBar supportActionBar = getSupportActionBar();
        if (supportActionBar != null) {
            supportActionBar.setDisplayHomeAsUpEnabled(backPressed);
        }
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });


        toolbar.setContentInsetStartWithNavigation(0);

    }


    public void onBackPressed() {
        super.onBackPressed();
        if (existActivityWithAnimation) {
            overridePendingTransition(R.anim.slide_in_left, R.anim.side_out_right);
        }
    }
}
